﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.IO;
using System.Web.Hosting;

namespace WPFInterface.ViewModel
{
    public class AddPatientViewModel : BaseViewModel
    {
        private bool _closeSignal;
        private ICommand _addCommand;
        private string _name;
        private string _firstname;
        private int _id;
        private string _birthdayYear;
        private string _birthdayMonth;
        private string _birthdayDay;
        private HomeViewModel hvm;

        public HomeViewModel Hvm
        {
            get { return hvm; }
            set { hvm = value; }
        }

        public string BirthdayYear
        {
            get { return _birthdayYear; }
            set { _birthdayYear = value; }
        }

        public string BirthdayMonth
        {
            get { return _birthdayMonth; }
            set { _birthdayMonth = value; }
        }

        public string BirthdayDay
        {
            get { return _birthdayDay; }
            set { _birthdayDay = value; }
        }

        public int Id
        {
            get { return _id; }
            set { _id = value; }
        }


        public string Firstname
        {
            get { return _firstname; }
            set { _firstname = value; }
        }

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }


        /// <summary>
        /// indique si on doit fermer la fenêtre ou non
        /// </summary>
        public bool CloseSignal
        {
            get { return _closeSignal; }
            set
            {
                if (_closeSignal != value)
                {
                    _closeSignal = value;
                    OnPropertyChanged("CloseSignal");
                }
            }
        }

        /// <summary>
        /// command pour s'authentifier
        /// </summary>
        public ICommand AddCommand
        {
            get { return _addCommand; }
            set { _addCommand = value; }
        }

        public AddPatientViewModel()
        {
            
            _addCommand = new RelayCommand(param => AddAccess(), param => true);
        }

        private void AddAccess()
        {
            ServicePatient.ServicePatientClient pat = new ServicePatient.ServicePatientClient();
            Dbo.Patient newpat = new Dbo.Patient();
            newpat.Name = Name;
            newpat.Firstname = Firstname;
            newpat.Id = pat.GetListPatient().Last<Dbo.Patient>().Id + 1;
            newpat.Birthday = new DateTime(Int32.Parse(BirthdayYear), Int32.Parse(BirthdayMonth), Int32.Parse(BirthdayDay));
            newpat.Observations = new List<Dbo.Observation>();
            Hvm.ListPatients.Add(newpat);
            pat.AddPatient(newpat);
            this.CloseSignal = true;
        }
    }
}
